<?php
/**
 * Representation of a function parameter
 */
class My_Code_Function_Parameter
{
    const NO_DEFAULT_VALUE = 'noDefaultValue';
    protected $name;
    protected $value = My_Code_Function_Parameter::NO_DEFAULT_VALUE;
    protected $type;
    protected $comment;

    protected function  __construct()
    {
    }

    /**
     * @return My_Code_Function_Parameter
     */
    public static function factory()
    {
        return new self();
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param string $name
     * @return My_Code_Function_Parameter
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getValue()
    {
        return $this->value;
    }

    /**
     * @param mixed $value
     * @return My_Code_Function_Parameter
     */
    public function setValue($value)
    {
        $this->value = $value;
        return $this;
    }

    /**
     * @return string
     */
    public function getType()
    {
        if (is_null($this->type)) 
        {
            return $this->tryToDiscoverType();
        }
        return $this->type;
    }

    /**
     * @return string
     */
    protected function tryToDiscoverType()
    {
        if (is_string($this->value) && My_Code_Function_Parameter::NO_DEFAULT_VALUE == $this->getValue()) return 'string';
        elseif (is_integer ($this->value)) return 'integer';
        elseif (is_float ($this->value)) return 'float';
        elseif (is_bool ($this->value)) return 'boolean';
        else return 'mixed';
    }

    /**
     * @param string $type
     * @return My_Code_Function_Parameter
     */
    public function setType($type)
    {
        $this->type = $type;
        return $this;
    }
    
    /**
     * @param string $comment
     * @return My_Code_Function_Parameter 
     */
    public function setComment($comment)
    {
        $this->comment = $comment;
        return $this;
    }

    /**
     * @return string
     */
    public function getDocLine()
    {
        $docLine = "@param " . $this->getType() . ' $' . $this->getName();
        if (!is_null($this->comment)) $docLine .= ' '. $this->comment;
        return $docLine;
    }
}
